﻿Public Class frmEditVendor
    Dim Strsql As String
    Dim _Mysql As New ClsSQLhelper
    Dim On_Load As Boolean = True
    Public Primary_Key As Integer
    Public Section_ID As Integer
    Dim DTProvince, DTAMPHUR, DTTmb As New DataTable

#Region "Funtion"
  
   Sub Load_Vendor_Type()
        Try
            Strsql = "select * from vendor_type"
            Dim DT_vendor_type As DataTable = _Mysql.GetMYSQLDataTable(Strsql, "vendor_type")
            With CboCompanyType
                .DataSource = DT_vendor_type
                .DisplayMember = "vendor_type_name"
                .ValueMember = "vendor_type_id"
            End With
        Catch ex As Exception

        End Try
    End Sub

    Sub Load_AddressData(Optional ByVal siteid As Integer = 0, Optional ByVal Str_Search As String = "")
        Try

        
            Strsql = "Select province_code,province_name from tblprovince"
            DTProvince = _Mysql.GetMYSQLDataTable(Strsql, "tblprovince")
            With Me.CboProvince
                .DropDownStyle = ComboBoxStyle.DropDownList
                .DataSource = DTProvince
                .DisplayMember = "province_name"
                .ValueMember = "province_code"
            End With
            Strsql = "Select amphur_code,amphur_name from tblamphur where amphur_code like '" & Me.CboProvince.SelectedValue.ToString & "%'"
            DTAMPHUR = _Mysql.GetMYSQLDataTable(Strsql, "tblAMPHUR")

            With Me.cboAMPHUR
                .DropDownStyle = ComboBoxStyle.DropDownList
                .DataSource = DTAMPHUR
                .DisplayMember = "amphur_name"
                .ValueMember = "amphur_code"
            End With
            Strsql = "Select distric_code,ditrict_name from tbldistrict  where distric_code like '" & Me.cboAMPHUR.SelectedValue.ToString & "%'"
            DTTmb = _Mysql.GetMYSQLDataTable(Strsql, "tbldistrict")

            With Me.cboTmb
                .DropDownStyle = ComboBoxStyle.DropDownList
                .DataSource = DTTmb
                .DisplayMember = "ditrict_name"
                .ValueMember = "distric_code"
            End With
            ' Cboonload = False
            On_load = False

        Catch ex As Exception

        End Try
    End Sub
    Sub Load_Data()
        Try
            Strsql = "select * from vendorcompany  where companyid = " & Primary_Key


            Dim DT_Companny As New DataTable
            DT_Companny = _Mysql.GetMYSQLDataTable(Strsql, "companyname")
            With DT_Companny
                Me.txtCompanyname.Text = .Rows(0).Item("companyname").ToString
                Me.CboCompanyType.SelectedValue = .Rows(0).Item("companytype")
                Me.TxtContactname.Text = .Rows(0).Item("contactname").ToString
                Me.TxtTaxID.Text = .Rows(0).Item("taxid").ToString
                Me.TxtTel1.Text = .Rows(0).Item("tel").ToString
                Me.TxtFax1.Text = .Rows(0).Item("fax").ToString
                Me.TxtNote.Text = .Rows(0).Item("note").ToString
                Me.ChkActive.Checked = .Rows(0).Item("active").ToString
                Me.TxtAdd1.Text = .Rows(0).Item("address1").ToString
                Me.cboTmb.SelectedValue = .Rows(0).Item("tmppart")
                Me.cboAMPHUR.SelectedValue = .Rows(0).Item("amppart")
                Me.CboProvince.SelectedValue = .Rows(0).Item("chwpart")
                Me.TxtZipcode.Text = .Rows(0).Item("zipcode").ToString

            End With
        Catch ex As Exception

        End Try
    End Sub
#End Region
    Private Sub EditVendor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.TxtCode.Text = Primary_Key
        Load_Vendor_Type()
        Load_AddressData()
        Load_Data()
    End Sub

    Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        Try


            Strsql = "update vendorcompany set contactname = '" & Me.TxtContactname.Text & "',companytype=" & Me.CboCompanyType.SelectedValue & ",companyname= '" & Me.txtCompanyname.Text & "',taxid = '" & Me.TxtTaxID.Text & "', address1 = '" & Me.TxtAdd1.Text & "',tmppart = " & Me.cboTmb.SelectedValue & ",amppart = " & Me.cboAMPHUR.SelectedValue
            Strsql = Strsql & " ,chwpart = " & Me.CboProvince.SelectedValue & ",zipcode = '" & Me.TxtZipcode.Text & "',tel = '" & Me.TxtTel1.Text & "',fax = '" & Me.TxtFax1.Text & "'"
            Strsql = Strsql & " , note = '" & Me.TxtNote.Text & "' ,active = " & Me.ChkActive.Checked
            Strsql = Strsql & " where companyid = '" & Primary_Key & "'"

            _Mysql.MySQLExecute(Strsql)
            'DA_AGENT.Update(DSWHIS.Tables("vendoragent"))
            'DA_Products.Update(DSWHIS.Tables("vendorproducts"))

            'frmSupplyManagement.Load_Data()
            MsgBox("ปรับปรุงข้อมูลเข้าเรียบร้อยแล้ว", MsgBoxStyle.Information)
            Me.DialogResult = Windows.Forms.DialogResult.OK
        Catch ex As Exception

        End Try
    End Sub

    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Me.Dispose()
    End Sub

    Private Sub CboProvince_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboProvince.SelectedIndexChanged
        Try
            If On_Load = True Then Exit Sub
            Strsql = "Select amphur_code,amphur_name from tblamphur where amphur_code like '" & Me.CboProvince.SelectedValue.ToString & "%'"
            DTAMPHUR = _Mysql.GetMYSQLDataTable(Strsql, "tblAMPHUR")

            With Me.cboAMPHUR
                .DropDownStyle = ComboBoxStyle.DropDownList
                .DataSource = DTAMPHUR
                .DisplayMember = "amphur_name"
                .ValueMember = "amphur_code"
            End With
            Strsql = "Select distric_code,ditrict_name from tbldistrict  where distric_code like '" & Me.cboAMPHUR.SelectedValue.ToString & "%'"
            DTTmb = _Mysql.GetMYSQLDataTable(Strsql, "tbldistrict")

            With Me.cboTmb
                .DropDownStyle = ComboBoxStyle.DropDownList
                .DataSource = DTTmb
                .DisplayMember = "ditrict_name"
                .ValueMember = "distric_code"
            End With
            ' Cboonload = False
        Catch ex As Exception

        End Try
    End Sub

    Private Sub cboAMPHUR_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboAMPHUR.SelectedIndexChanged
        Try
            If On_Load = True Then Exit Sub
            Strsql = "Select distric_code,ditrict_name from tbldistrict  where distric_code like '" & Me.cboAMPHUR.SelectedValue.ToString & "%'"
            DTTmb = _Mysql.GetMYSQLDataTable(Strsql, "tbldistrict")

            With Me.cboTmb
                .DropDownStyle = ComboBoxStyle.DropDownList
                .DataSource = DTTmb
                .DisplayMember = "ditrict_name"
                .ValueMember = "distric_code"
            End With
        Catch ex As Exception

        End Try
    End Sub
End Class